using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class tank : MonoBehaviour
{
    public bool selected = false;
    private float ball_time = 0;
    private GameObject bullet;
    public GameObject cell_old = null;
    public int harm = 20;
    public int speed;
    public Material material_origin;
    // Start is called before the first frame update
    void Start()
    {
        
        bullet = (GameObject)Resources.Load("bullet");
        material_origin = new Material(gameObject.transform.GetComponent<Renderer>().material);
    }

    // Update is called once per frame
    void Update()
    {
        ball_time += 0.2f;
        GameObject enemy_now = auto_detect(5);
        if (enemy_now != null)
        {
            Vector3 enemy_position = new Vector3(enemy_now.transform.position.x, transform.position.y, enemy_now.transform.position.z);
            gameObject.transform.LookAt(enemy_position);
            fire();

        }
        if (Input.GetMouseButton(1))
        {
            //选中所有的炮台，让他们每一个的selected都设置为false，同时把颜色改为初始颜色
            foreach (GameObject element in GameObject.FindGameObjectsWithTag("tank"))
            {
                element.GetComponent<tank>().selected = false;
                element.transform.GetComponent<Renderer>().material.color = material_origin.color;
                element.transform.Find("Gun").GetComponent<Renderer>().material.color = material_origin.color;
            }
        }
        if (selected == true)
        {
            //如果炮台的selected变量为true,执行{}中的语句
            foreach (GameObject element in GameObject.FindGameObjectsWithTag("cell"))
            {
                //使用foreach循环遍历所有标签为cell的游戏对象    
                if (element.GetComponent<cell>().selected == true && element.GetComponent<cell>().is_useful == true)
                {
                    //如果当前遍历到的cell游戏对象的selected变量也为true,并且is_useful变量为true,执行{}中的语句  

                    //获取当前cell的x坐标
                    float base_x = element.transform.position.x;
                    //获取当前cell的z坐标  
                    float base_z = element.transform.position.z;
                    //获取当前炮台的y坐标(高度)         
                    float tank_y = gameObject.transform.position.y;
                    //将炮台移动到当前cell的位置     
                    gameObject.transform.position = new Vector3(base_x, tank_y, base_z);

                    //将炮台的selected变量设置为false
                    selected = false;
                    //将炮台Renderer的材质设置为默认材质   
                    gameObject.transform.GetComponent<Renderer>().material.color = material_origin.color;
                    gameObject.transform.Find("Gun").GetComponent<Renderer>().material.color = material_origin.color;

                    //将当前遍历到的cell的selected变量设置为false
                    element.GetComponent<cell>().selected = false;
                    //将当前遍历到的cell的is_useful变量设置为false
                    element.GetComponent<cell>().is_useful = false;

                    if (cell_old != null)
                    {
                        //如果cell_old变量不为null,将其is_useful变量设置为true
                        cell_old.GetComponent<cell>().is_useful = true;
                    }
                    //将当前遍历到的cell赋值给cell_old变量
                    cell_old = element;
                }
            }
        }
    }
    private GameObject auto_detect(float _fire_range_)
    {
        //自动检测敌人方法,参数为射程

        float distance_min = 100;
        //初始化最近敌人距离

        GameObject enemy_target = null;
        //初始化敌人目标  

        GameObject[] enemy_array = GameObject.FindGameObjectsWithTag("enemy");
        //找到所有标签为enemy的游戏对象,存储在数组enemy_array中

        foreach (GameObject element in enemy_array)
        {
            //使用foreach循环遍历enemy_array数组中的所有游戏对象

            float distance_now = Vector3.Distance(transform.position, element.transform.position);
            //计算当前游戏对象与敌人的距离  

            if (distance_now < _fire_range_)
            {
                //如果当前计算出的距离小于射程,执行{}中的语句             

                if (distance_now < distance_min)
                {
                    //如果当前距离小于之前记录的最近距离,执行{}中的语句

                    distance_min = distance_now;
                    //更新最近距离 
                    enemy_target = element;
                    //更新敌人目标
                }
            }
        }
        return enemy_target;
        //返回敌人目标
    }

    private void fire()
    {
        //射击方法
        if (ball_time > 10)
        {
            //如果ball_time大于10,可以射击
            GameObject ball_new = Instantiate(bullet);
            //实例化bullet预制体
            ball_new.transform.position = gameObject.transform.Find("fire").transform.position;
            //设置ball_new的位置为fire点的位置  
            ball_new.transform.eulerAngles = gameObject.transform.eulerAngles;
            //设置ball_new的旋转角度为炮台的旋转角度 
            ball_new.GetComponent<bullet_ball>().distance = 0.5f;
            //设置ball_new的ball脚本的distance变量为0.5  
            ball_time = 0;     //将ball_time重置为0
        }
    }

    private void OnDrawGizmos()
    {
        //使用Gizmos绘制一个蓝色的线框球体
        Gizmos.color = Color.blue;
        //设置Gizmo的颜色为蓝色

        Gizmos.DrawWireSphere(transform.position, 5);
        //在物体的位置绘制半径为5的线框球体
    }

    //点击炮台,让其处于选中状态,把其他炮台的选中状态取消
    private void OnMouseDown()
    {
        //选中所有的炮台,让他们每一个的selected都设置为false,同时把颜色改为初始颜色
        foreach (GameObject element in GameObject.FindGameObjectsWithTag("tank"))
        {
            element.GetComponent<tank>().selected = false;
            element.transform.GetComponent<Renderer>().material.color = material_origin.color;
            element.transform.Find("Gun").GetComponent<Renderer>().material.color = material_origin.color;
        }
        //把选中的自己的selected设置为true,同时颜色改为red
        gameObject.transform.GetComponent<Renderer>().material.color = Color.red;
        gameObject.transform.Find("Gun").GetComponent<Renderer>().material.color = Color.red;
        selected = true;
    }

}
